<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");

class Admin_model extends CI_Model {

    public $tb_group = "admin_group";
    public $tb_acc = "admin";

    public function __construct() {
        parent::__construct();
    }

    //lay tong tai khoan
    public function SumTK($cstatus = null) {
        $this->db->select("AdminID");
        if ($cstatus != null) {
            $this->db->where("AdminStatus", $cstatus);
        }
        $result = $this->db->get($this->tb_acc);
        if ($result->num_rows() > 0) {
            return $result->num_rows();
        } else {
            return $result = 0;
        }
    }

    //lay thong tin tai khoan
    public function getInfoAdmin($id) {
        if (isset($id)) {
            $this->db->where("AdminID", $id);
        }
        $q = $this->db->get($this->tb_acc);
        if ($q->num_rows() > 0) {
            return $q;
        } else {
            return FALSE;
        }
    }

    //xac nhan tk theo ten
    public function checkAdminName($name) {
        if (isset($name) && $name != null) {
            $this->db->where("AdminUser", $name);
        }
        $q = $this->db->get($this->tb_acc);
        if ($q->num_rows() > 0) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    //xac nhan email da ton tai
    public function checkAdminEmail($email) {
        if (isset($email) && $email) {
            $this->db->where("AdminEmail", $email);
        }
        $q = $this->db->get($this->tb_acc);
        if ($q->num_rows() > 0) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    //insert account
    public function createAcc($data) {
        $arr = array(
            "AdminName" => $data["name"],
            "AdminUser" => $data["user"],
            "AdminPass" => $data["pass"],
            "AdminEmail" => $data["email"],
            "admin_group_id" => $data["group"]
        );
        $this->db->set($arr);
        if ($this->db->insert($this->tb_acc)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //lay cac tai khoan tru tai khoan cao nhat
    public function getInforMod() {
        $q = $this->db->where("AdminUser != 'admin'")
                ->get("admin");
        if ($q->num_rows() > 0) {
            return $q;
        }
        return $q;
        $q->free_result();
    }

    //login
    public function login($user, $pass) {
        if ($user === NULL && $pass === NULL) {
            return FALSE;
        } else {
            $this->db->select("AdminID,AdminName,AdminUser,AdminPass,admin_group_id");
            $this->db->where("AdminUser", $user);
            $this->db->where("AdminPass", hash("sha256", $pass));
            $query = $this->db->get($this->tb_acc);
            if ($query->num_rows() === 1) {
                return $query->result();
                $query->free_result();
            } else {
                return FALSE;
            }
        }
    }

    public function read($id) {
        $this->db->where('admin_group_id', $id);

        $query = $this->db->get($this->tb_group);

        return $query->row();
    }

    public function create($data) {
        $arr = array('admin_group_name' => $data['name'], 'permission_view' => $data['permis_view'], 'permission_edit_del' => $data['permis_edit_delete']);

        $this->db->set($arr);
        if ($this->db->insert($this->tb_group)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function update($id, $data) {
        $this->db->where('admin_group_id', $id);
        $arr = array('admin_group_name' => $data['name'], 'permission_view' => $data['permis_view'], 'permission_edit_del' => $data['permis_edit_delete']);

        $this->db->set($arr);

        if ($this->db->update($this->tb_group))
            return TRUE; else
            return FALSE;
    }

    public function getGroup() {
        $q = $this->db->select("*")
                ->from("admin_group")
                ->get();
        return $q;
        $q->free_result();
    }

    //xoa group
    public function deleteG($id) {
        $this->db->where("admin_group_id", $id);
        $q = $this->db->delete("admin_group");
        if ($q) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //sua acc
    public function editAcc($id, $data) {
        $this->db->where('AdminID', $id);
        $arr = array(
            'AdminName' => $data['name'],
            'AdminPass' => $data['pass'],
            "AdminEmail" => $data["email"]
        );
        $this->db->set($arr);

        if ($this->db->update($this->tb_acc))
            return TRUE; else
            return FALSE;
    }

    //xoa tai khoan
    public function deleteAcc($id) {
        $this->db->where("AdminID", $id);
        $q = $this->db->delete($this->tb_acc);
        if ($q) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    //sua group cho user
    public function editGAcc($id,$gid){
        $this->db->where("AdminID",$id);
        $arr = array(
          "admin_group_id" => $gid  
        );
        
        $this->db->set($arr);
        if($this->db->update($this->tb_acc)){
            return TRUE;
        }else{
            return FALSE;
        }
    }
}

?>
